package wnhz.com.business.groupshop.adapter;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.google.gson.Gson;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import wnhz.com.business.R;
import wnhz.com.business.base.BaseRecyclerAdapter;
import wnhz.com.business.base.RecyclerViewHolder;
import wnhz.com.business.groupshop.bean.OrderBean;
import wnhz.com.business.impl.ITaskFinishListener;
import wnhz.com.business.uitls.LogUtils;
import wnhz.com.business.uitls.MyCallBack;
import wnhz.com.business.uitls.Url;
import wnhz.com.business.uitls.XUtil;

/**
 * Created by Administrator on 2017/6/29 0029.
 */

public class FragmentOneAdapter extends BaseRecyclerAdapter<OrderBean.InfoBean> {
    static final String TAG = "CollectAdapter";

    private MyItemClickLinstener ClickLinstener;
    private ProgressDialog mProgressDialog;
    private OrderBean orderBean;
    private List<OrderBean.InfoBean> infoList;
    private LinearLayout layContent;
    private String copewith_price;


    public FragmentOneAdapter(Context context, List<OrderBean.InfoBean> datas) {
        super(context, datas, R.layout.item_one_layout);
    }

    public void setOnClickListener(MyItemClickLinstener listener) {
        ClickLinstener = listener;
    }

    @Override
    protected void loadData(int page, final ITaskFinishListener iTaskFinishListener) {
        if (mProgressDialog == null) {
            mProgressDialog = new ProgressDialog(context);
            mProgressDialog.setCancelable(true);
            mProgressDialog.setCanceledOnTouchOutside(false);
        }
        mProgressDialog.setMessage("加载中...");
        mProgressDialog.show();

        SharedPreferences sp = context.getSharedPreferences("user", Context.MODE_PRIVATE);
        String token = sp.getString("token", "");
        Map<String, Object> map = new HashMap<>();
        map.put("token", token);
        map.put("type", "1");  //代付款

        XUtil.Post(Url.ORDER_MYORDER, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                LogUtils.e("----------代付款-----", result);
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    String re = jsonObject.optString("re");
                    String url = jsonObject.optString("url");
                    if (re.equals("1")) {
                        Gson gson = new Gson();
                        orderBean = gson.fromJson(result, OrderBean.class);
                        infoList = orderBean.getInfo();
                        iTaskFinishListener.onSuccess(TAG, infoList);
                    } else if (re.equals("0")) {
                        iTaskFinishListener.onSuccess(TAG, infoList);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                    if (mProgressDialog != null) {
                        mProgressDialog.dismiss();
                    }
                }
            }

            @Override
            public void onFinished() {
                super.onFinished();
                if (mProgressDialog != null) {
                    mProgressDialog.dismiss();
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
                if (mProgressDialog != null) {
                    mProgressDialog.dismiss();
                }
            }
        });
    }


    @Override
    protected void bindViewHolder(final int position, RecyclerViewHolder holder) {
        TextView tv_cancle = holder.getView(R.id.tv_cancle, false);
        TextView tv_amountprice = holder.getView(R.id.tv_amountprice, false);
        TextView tv_amount = holder.getView(R.id.tv_amount, false);
        TextView tv_order_haoma = holder.getView(R.id.tv_order_haoma, false);
        TextView tv_pay = holder.getView(R.id.tv_pay, false);

        RecyclerView recycler = holder.getView(R.id.recycler, false);

        //横向滑动布局
        layContent = holder.getView(R.id.lay_content, false);

        final OrderBean.InfoBean item = getItem(position);
        final List<OrderBean.InfoBean.GoodsBean> listOrder = item.getGoods();

        tv_amount.setText("共" + item.getGoods().size() + "件商品  合计: ");
        tv_amountprice.setText("¥ " + item.getCopewith_price());
        tv_order_haoma.setText("订单号:" + item.getOrder_no());

        //去支付的应付金额
        copewith_price = orderBean.getInfo().get(position).getCopewith_price();

        tv_cancle.setTag(R.id.tv_cancle, position);
        tv_cancle.setOnClickListener(itemClickLinstener);

        tv_pay.setTag(R.id.tv_pay, position);
        tv_pay.setOnClickListener(itemClickLinstener);
    }

    @Override
    protected void setPageSize(int pageSize) {

    }

    private View.OnClickListener itemClickLinstener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (ClickLinstener != null) {
                int position;
                switch (v.getId()) {
                    case R.id.tv_cancle:
                        position = (int) v.getTag(R.id.tv_cancle);
                        LogUtils.e("==============" + TAG, "---------------------------------" + position);
                        ClickLinstener.OnCancleClickListener(FragmentOneAdapter.this, position, v);
                        break;
                    case R.id.tv_pay:
                        position = (int) v.getTag(R.id.tv_pay);
                        LogUtils.e("==============" + TAG, "---------------------------------" + position);
                        ClickLinstener.OnPayClickListener(FragmentOneAdapter.this, position, copewith_price, v);
                        break;
                }
            }
        }
    };


    public interface MyItemClickLinstener {
        void OnCancleClickListener(FragmentOneAdapter adapter, int position, View view);

        void OnPayClickListener(FragmentOneAdapter adapter, int position, String money, View view);
    }
}
